package Function;


import FCRendering.FCMapAnimationConstant;
import FCRendering.FCRMaster;
import VWorldGameStudio.FCEvent;
import VWorldGameStudio.FCMapAnimation;
import VWorldGameStudio.events.JumpEvent;
import VWorldGameStudio.events.StepOnEvent;
import com.google.gson.Gson;
import io.reactivex.schedulers.Schedulers;
import socket.FCRSocketClient;
import socket.FCRSocketServer;
import utils.ClickHelper;
import utils.entity.EventCmdEntity;

import java.util.concurrent.TimeUnit;

import static FCRendering.FCRMaster.*;


//自动触发型事件的处理
public class FCAutoTriggerEventHelper {
    Gson gson = new Gson();

    private String lastEventName;
    private ClickHelper clickHelper = new ClickHelper(500);

    public void dealAutoTriggerFcEvents(FCEvent event) {
        if (!event.getType().equals("stepOn")) {
            return;
        }
        StepOnEvent stepOnEvent = (StepOnEvent)event;
       System.out.println("getContainCommand " + stepOnEvent.getContainCommand());
        lastEventName = event.getName();
        EventCmdEntity entity = gson.fromJson(stepOnEvent.getContainCommand(), EventCmdEntity.class);
        switch (entity.cmdType) {
            case EventCmdEntity.CmdTypeSaveChuansong:
                String mapName = player1.getMapname();
                if(entity.mapName != null){
                    mapName = entity.mapName;
                }
                if (stepOnEvent.getContainOtherEventId() != null) {
                    // if (!gameSave.getChuanZhenMaps().containsKey(mapName)) {
                    gameSave.getChuanZhenMaps().put(mapName, stepOnEvent.getContainOtherEventId());
                    //   }
                }

                if(entity.containOtherEventId_2 != null){
                    gameSave.getTankChuanZhenMaps().put(mapName, entity.containOtherEventId_2);
                }
                break;
            case EventCmdEntity.CmdTypeStartChuansong:
                if (!fcv.isInTeamLink()) {
                    if (fcv.getWillChuangSongMapName() != null) {
                        if (clickHelper.canClick()) {
                            clickHelper.setClickLock(true);
                            //等待一秒再传走哈哈哈
                            String jumpEventId = gameSave.getChuanZhenMaps().get(fcv.getWillChuangSongMapName());
                            System.out.println("jumpEventId "+jumpEventId);
                            FCEvent event1 = eventLib.getEventOfName(jumpEventId);
                            if(event1 == null && !event1.getType().equals("jump")){
                                clickHelper.setClickLock(false);
                                return;
                            }
                            JumpEvent jumpEvent = (JumpEvent)event1;
                            fcv.setPlayingEvent(jumpEvent);
                            sound.setEffectSound(jumpEvent.getMusicnum0());
                            fcv.setJumpactionnum(0);
                            //TODO 通知队友也要转场了
                            if (fcv.isSever()) {
                                FCRSocketServer.getIn().playerJumpEvent(fcv.getPlayingEvent().getName(),jumpEvent.getDestination());
                            } else if (fcv.isClient()) {
                                FCRSocketClient.getIn().playerJumpEvent(fcv.getPlayingEvent().getName(), jumpEvent.getDestination());
                            }
                            fcv.setWillChuangSongMapName(null);

                            Schedulers.io().scheduleDirect(new Runnable() {
                                @Override
                                public void run() {
                                    FCMapAnimation chuanzhenAni = mapAnimationLibrary.getMapAnimationByName(FCMapAnimationConstant.ChuanZhenName);
                                    if (chuanzhenAni != null) {
                                        chuanzhenAni.setVisiable(0);
                                    }
                                    clickHelper.setClickLock(false);
                                }
                            }, 3, TimeUnit.SECONDS);
                        }
                    }
                }
                break;
        }
    }

}
